package org.arnetminer.tapestry.pages.user;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.arnetminer.tapestry.dao.UserDAO;
import org.arnetminer.tapestry.entities.User;
import org.arnetminer.tapestry.pages.Index;

/**
 * Start page of application tapestry.
 */
public class CreateUser
{

	@Property
    private String usr;
	@Property
    private String pwd;

    @Inject
	private UserDAO usrDao;
    
    @Inject
    private Messages messages;
	
    @InjectPage
    private Index index;
    
    
    @Component(id = "pwd")
    private PasswordField passwordField;

    @Component
    private Form form;

    private User user;

    @CommitAfter
    Object onSuccess()
    {
    	user = new User();
    	user.setPwd(pwd);
    	user.setUsr(usr);
    	if (!usrDao.isExist(user)) {
    		usrDao.save(user);
    		return index;
    	}
    	form.recordError(messages.get("error"));
        return null;
    }
}
